package com.example.demo.controller;

import com.alibaba.fastjson.JSONArray;
import com.example.demo.mapper.MusicMapper;
import com.example.demo.pojo.Music;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class DataController {

    @Autowired
    MusicMapper musicMapper;

    private Map<Object, Object> commonajax(Integer code, String msg, List<Map<Object, Object>> data){
        Map<Object, Object> map = new HashMap<>();
        map.put("code", code);
        map.put("msg",msg);
        map.put("count",10000);
        map.put("data", data);
        return map;
    }

    @RequestMapping(value = "/music", method = RequestMethod.GET)
    public String hello(@RequestParam("page")Integer page, @RequestParam("limit")Integer limit) {
        List<Music> list;
        //System.out.println("page:"+page+"limit:"+limit);
//        Map<Object, Object> map1 = new HashMap<Object, Object>();
//        map1.put("id", "10000");
//        list.add(map1);
//        Map<Object, Object> map2 = new HashMap<Object, Object>();
//        map2.put("id","10002");
//        list.add(map2);
        int size = musicMapper.getCount();
        list = musicMapper.findByPage((page-1)*limit,limit);
        //Map<Object, Object> map=this.commonajax(0,"查询成功",list);
        String s = JSONArray.toJSON(list).toString();
        //System.out.println(s);
        return "{\"code\":0 , \"data\":"+s+", \"count\":"+size+"}";
    }
}
